Page({
  data: {
    faceCareVideos: [],
    makeupTutorialVideos: [],
    headTutorialVideos: []
  },

  onShow: function() {
    this.getVideoData()
  },

  getVideoData: function() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    
    wx.cloud.callFunction({
      name: 'video',
      success: res => {
        if (res.result.code === 0) {
          const videos = res.result.data
          const faceCare = []
          const makeupTutorial = []
          const headTutorial = []
          
          videos.forEach(item => {
            if (item.style === '脸部护理') {
              faceCare.push(item)
            } else if (item.style === '妆容教程') {
              makeupTutorial.push(item)
            } else if (item.style === '毛发教程') {
              headTutorial.push(item)
            }
          })
          
          this.setData({
            faceCareVideos: faceCare.slice(0, 3),
            makeupTutorialVideos: makeupTutorial.slice(0, 3),
            headTutorialVideos: headTutorial.slice(0, 3),
            fullFaceCareVideos: faceCare,
            fullMakeupTutorialVideos: makeupTutorial,
            fullHeadTutorialVideos: headTutorial
          }, () => {
            wx.hideLoading();
          });
        }
      },
      fail: err => {
        console.error('获取视频数据失败:', err)
        wx.hideLoading();
      }
    })
  },
  
  playVideo: function(e) {
    const videoId = e.currentTarget.dataset.id;
    this.setData({ currentVideoId: videoId });
  },
  
  navigateToFaceCare: function(e) {
    wx.navigateTo({
      url: `/pages/videoDetail/videoDetail?title=脸部护理&videos=${JSON.stringify(this.data.fullFaceCareVideos)}`
    });
  },
  
  navigateToMakeupTutorial: function(e) {
    wx.navigateTo({
      url: `/pages/videoDetail/videoDetail?title=妆容教程&videos=${JSON.stringify(this.data.fullMakeupTutorialVideos)}`
    });
  },

  navigateToHeadTutorial: function(e) {
    wx.navigateTo({
      url: `/pages/videoDetail/videoDetail?title=毛发教程&videos=${JSON.stringify(this.data.fullHeadTutorialVideos)}`
    });
  }
});